package com.htht.smartschool.service.consume;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.htht.smartschool.common.service.BaseService;
import com.htht.smartschool.model.consume.TConsume;
import com.htht.smartschool.util.system.DateUtil;

public class ConsumeService extends BaseService {
	
	public TConsume addConsume(TConsume tConsume){
		return (TConsume) getDao().create(tConsume);
	}
	
	public boolean deleteConsume(TConsume tConsume){
		return getDao().delete(tConsume);
	}
	
	public TConsume updateConsume(TConsume tConsume){
		return (TConsume) getDao().update(tConsume);
	}
	
	public TConsume getConsume(TConsume tConsume){
		String hql = "from TConsume where studentcode='"+tConsume.getStudentcode()+"' ORDER BY TIME DESC";
		TConsume tc =(TConsume) getDao().queryFirstByHQL(hql);
		Long start = null;
		Long end = null;
		Date now = new Date();
		SimpleDateFormat sdf = null; 
		Double total = 0.00;
		String nowday ="";
		//day
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		nowday = sdf.format(now)+" 00:00:00";
		start = DateUtil.getDateByStr(nowday).getTime()/1000;
		end = now.getTime()/1000;
		total =getSumConsume(tConsume.getStudentcode(),start,end);
		tc.setDay(total);
		//month
		sdf = new SimpleDateFormat("yyyy-MM");
	    nowday = sdf.format(now)+"-01 00:00:00";
		start = DateUtil.getDateByStr(nowday).getTime()/1000;
		end = now.getTime()/1000;
		total =getSumConsume(tConsume.getStudentcode(),start,end);
		tc.setMonth(total);
		//year
		sdf = new SimpleDateFormat("yyyy");
		nowday = sdf.format(now)+"-01-01 00:00:00";
		start = DateUtil.getDateByStr(nowday).getTime()/1000;
		end = now.getTime()/1000;
		total =getSumConsume(tConsume.getStudentcode(),start,end);
		tc.setYear(total);
		return tc;
	}
	@SuppressWarnings("rawtypes")
	public Double getSumConsume(String studentcode,Long start,Long end){
		String sql = "SELECT IFNULL(SUM(consume),0.00) FROM t_consume WHERE studentcode ='"+studentcode+"' "
				+ "and time <= '"+end+"' and time >= '"+start+"'";
		List list = getDao().queryBySQL(sql);
		if(list.size() == 0){
			return null;
		}else{
			Double obj = (Double) list.get(0);
			return obj;
		}
	}
	@SuppressWarnings("unchecked")
	public List<TConsume> getConsumeStatistics(TConsume tConsume) {
		Date now = new Date();
		SimpleDateFormat sdf = null; 
		List<TConsume> result = new ArrayList<TConsume>();
		String sql ="";
		List<Object[]> list = null;
		Long start = null;
		Long end = null;
		if(tConsume.getTimetype().equalsIgnoreCase("day")){
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			String nowday = sdf.format(now)+" 00:00:00";
			start = DateUtil.getDateByStr(nowday).getTime()/1000;
			end = now.getTime()/1000;
			sql = "FROM TConsume where studentcode=:studentcode and time <= '"+end+"' and time >= '"+start+"' ORDER BY TIME ASC";
			result = getDao().queryByHQL(sql,tConsume);
		}else if(tConsume.getTimetype().equalsIgnoreCase("month")){
			sdf = new SimpleDateFormat("yyyy-MM");
			String nowday = sdf.format(now)+"-01 00:00:00";
			start = DateUtil.getDateByStr(nowday).getTime()/1000;
			end = now.getTime()/1000;
			sql ="SELECT FROM_UNIXTIME(TIME,'%Y-%m-%d') days,SUM(consume) COUNT FROM t_consume WHERE studentcode ='"+tConsume.getStudentcode()+"'"
				+ " and time <= '"+end+"' and time >= '"+start+"' GROUP BY days";
			list = getDao().queryBySQL(sql);
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = list.get(i);
				TConsume tc = new TConsume();
				tc.setConsume(Double.valueOf(String.valueOf(obj[1])));
				tc.setTimestr(String.valueOf(obj[0]));
				result.add(tc);
			}
		}else if(tConsume.getTimetype().equalsIgnoreCase("year")){
			sdf = new SimpleDateFormat("yyyy");
			String nowday = sdf.format(now)+"-01-01 00:00:00";
			start = DateUtil.getDateByStr(nowday).getTime()/1000;
			end = now.getTime()/1000;
			sql ="SELECT FROM_UNIXTIME(TIME,'%Y-%m') months,SUM(consume) COUNT FROM t_consume WHERE studentcode ='"+tConsume.getStudentcode()+"' "
				+ " and time <= '"+end+"' and time >= '"+start+"' GROUP BY months; ";
			list = getDao().queryBySQL(sql);
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = list.get(i);
				TConsume tc = new TConsume();
				tc.setConsume(Double.valueOf(String.valueOf(obj[1])));
				tc.setTimestr(String.valueOf(obj[0]));
				result.add(tc);
			}
		}
		return result;
	}
}
